package com.bjfu.demo.task;

import com.alibaba.fastjson.JSON;
import com.bjfu.demo.dao.NewStatisticMapper;
import com.bjfu.demo.dao.SiteDataMapper;
import com.bjfu.demo.dto.StatisticRawResult;
import com.bjfu.demo.entity.SiteData;
import com.bjfu.demo.utils.MyTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class NewStoreSiteDetailTask {
  @Autowired private SiteDataMapper siteDataMapper1;
  @Autowired private NewStatisticMapper newStatisticMapper;
  @Autowired private StringRedisTemplate stringRedisTemplate;

  // 2022-01-01 04:00:00
  // 2023-01-01 04:00:00
  // 2024-01-01 04:00:00
  // 2025-01-01 04:00:00
  @Scheduled(cron = "0 0 4 1 1 ? ")
  public void storeStatisticResultByYear() {
    System.out.println("storeStatisticResultByYear");
    List<StatisticRawResult> yearResult =
        getStatisticResultByPeriod(
            MyTimeUtils.getLastYearBeginDate(), MyTimeUtils.getLastYearEndDate());
    stringRedisTemplate
        .opsForValue()
        .set("rp:AllStatisticResultByYear", JSON.toJSONString(yearResult));
  }

  // 2021-04-01 03:00:00
  // 2021-07-01 03:00:00
  // 2021-10-01 03:00:00
  @Scheduled(cron = "0 0 3 1 1,4,7,10 ? ")
  public void storeStatisticResultBySeason() {
    System.out.println("storeStatisticResultBySeason");
    List<StatisticRawResult> seasonResult =
        getStatisticResultByPeriod(
            MyTimeUtils.getLastSeasonBeginDate(), MyTimeUtils.getLastSeasonEndDate());
    stringRedisTemplate
        .opsForValue()
        .set("rp:AllStatisticResultBySeason", JSON.toJSONString(seasonResult));
  }

  // 2021-03-01 02:00:00
  // 2021-04-01 02:00:00
  // 2021-05-01 02:00:00
  @Scheduled(cron = "0 0 2 1 * ? ")
  public void storeStatisticResultByMonth() {
    System.out.println("storeStatisticResultByMonth");
    List<StatisticRawResult> monthResult =
        getStatisticResultByPeriod(
            MyTimeUtils.getLastMonthBeginDate(), MyTimeUtils.getLastMonthEndDate());
    stringRedisTemplate
        .opsForValue()
        .set("rp:AllStatisticResultByMonth", JSON.toJSONString(monthResult));
  }

  private List<StatisticRawResult> getStatisticResultByPeriod(Date beginDate, Date endDate) {
    List<StatisticRawResult> resultByPeriod =
        newStatisticMapper.getResultByPeriod(beginDate, endDate);
    for (StatisticRawResult statisticRawResult : resultByPeriod) {
      statisticRawResult.setEffectingRation(
          statisticRawResult.getEffectingNum()
              * 100
              / (statisticRawResult.getTotalNum() == 0 ? 1 : statisticRawResult.getTotalNum()));
    }
    resultByPeriod.sort(Comparator.comparingInt(StatisticRawResult::getEffectingRation).reversed());
    return resultByPeriod;
  }

  // 2021-03-01 00:01:00
  // 2021-04-01 00:01:00
  // 2021-05-01 00:01:00
  // 2021-06-01 00:01:00
  // 2021-07-01 00:01:00
  @Scheduled(cron = "0 1 0 1 1/1 ? ")
  public void storeStationInfo() {
    System.out.println("storeStationInfo");
    List<SiteData> siteData = siteDataMapper1.selectByExample(null);
    System.out.println(siteData);
    HashMap<String, StatisticRawResult> allStationInfo =
        new HashMap<>(); // 用来存放站点名称与所属地，键：code，值：StatisticRawResult，通用Vo
    for (SiteData siteDatum : siteData) {
      StatisticRawResult temp = new StatisticRawResult(); // 每一个temp存放一个站点的信息（名字、所属地）
      temp.setSiteCode(siteDatum.getSitecode());
      temp.setSiteName(siteDatum.getSitename());
      temp.setDistrict(siteDatum.getDistrict());
      allStationInfo.put(siteDatum.getSitecode(), temp);
    }
    stringRedisTemplate.opsForValue().set("rp:AllStationInfo", JSON.toJSONString(allStationInfo));
  }
}
